#include "stdafx.h"
#include "TestSiteTable.h"
#include "DatabaseBase.h"
#include "SiteTable.h"
#include "UEConnectionManager.h"

using namespace PDMS::PDMSDBIF;
CPPUNIT_TEST_SUITE_REGISTRATION( TestSiteTable );


TestSiteTable::TestSiteTable()
{
}


TestSiteTable::~TestSiteTable()
{
}

void TestSiteTable::TestInsert()
{
	DatabaseBase DB( GET_REGISTER( DatabaseElementFactory, Design_VersionOne ) );
	DB.DataSource( _T( "D:\\DesignDBTemplate.mdb" ) );
	UEConnectionPtr ptr = DB.DataSource();

	SiteTable* pSite = DB.GetSiteTable();
	ASSERT( pSite );

	UESoft::ErrorStatus es = pSite->Open();
	ASSERT( es == UESoft::eOk );

	pSite->NewRecord();
	pSite->SetID( 10 );
	pSite->SetParentID( 0 );
//	pSite->SetDescription( _T( "unset" ) );
	pSite->SetPurpose( _T( "unset" ) );
	pSite->SetFunction( _T( "unset" ) );
	pSite->SetNumber( 0 );
	pSite->SetArea( 0 );
//	pSite->SetModule( _T( "unset" ) );
	pSite->SetRlstored( _T( "Unset" ) );

	pSite->NewRecord();
	pSite->SetID( 8 );
//	pSite->SetParentID( 1 );
	pSite->SetDescription( _T( "H" ) );
	pSite->SetPurpose( _T( "E" ) );
	pSite->SetFunction( _T( "A" ) );
//	pSite->SetNumber( 0 );
//	pSite->SetArea( 0 );
	pSite->SetModule( _T( "R" ) );
	pSite->SetRlstored( _T( "T" ) );

	pSite->Update();

	pSite->Close();
	CPPUNIT_ASSERT( ptr != NULL );
	UEConnectionManager::Instance().CloseAllConnection();
}

void TestSiteTable::TestDelete()
{
	DatabaseBase DB( GET_REGISTER( DatabaseElementFactory, Design_VersionOne ) );
	DB.DataSource( _T( "D:\\DesignDBTemplate.mdb" ) );
	UEConnectionPtr ptr = DB.DataSource();

	SiteTable* pSite = DB.GetSiteTable();
	ASSERT( pSite );

	UESoft::ErrorStatus es = pSite->Open();
	ASSERT( es == UESoft::eOk );

	pSite->DeleteRecord( 4 );
	pSite->DeleteRecord( 8 );

	pSite->Update();

	pSite->Close();
	CPPUNIT_ASSERT( ptr != NULL );
	UEConnectionManager::Instance().CloseAllConnection();
}

void TestSiteTable::TestUpdate()
{
	DatabaseBase DB( GET_REGISTER( DatabaseElementFactory, Design_VersionOne ) );
	DB.DataSource( _T( "D:\\DesignDBTemplate.mdb" ) );
	UEConnectionPtr ptr = DB.DataSource();

	SiteTable* pSite = DB.GetSiteTable();
	ASSERT( pSite );

	UESoft::ErrorStatus es = pSite->Open();
	ASSERT( es == UESoft::eOk );

	pSite->UpdateRecord( 10 );
	pSite->SetID( 10 );
	pSite->SetParentID( 3 );
//	pSite->SetDescription( _T( "unset" ) );
	pSite->SetPurpose( _T( "Test" ) );
	pSite->SetFunction( _T( "unset" ) );
	pSite->SetNumber( 0 );
	pSite->SetArea( 0 );
//	pSite->SetModule( _T( "unset" ) );
	pSite->SetRlstored( _T( "Unset" ) );

	pSite->UpdateRecord( 1 );
//	pSite->SetID( 1 );
//	pSite->SetParentID( 1 );
	pSite->SetDescription( _T( "H" ) );
	pSite->SetPurpose( _T( "E" ) );
	pSite->SetFunction( _T( "A" ) );
//	pSite->SetNumber( 0 );
//	pSite->SetArea( 0 );
	pSite->SetModule( _T( "R" ) );
	pSite->SetRlstored( _T( "T" ) );

	pSite->Update();

	pSite->Close();
	CPPUNIT_ASSERT( ptr != NULL );
	UEConnectionManager::Instance().CloseAllConnection();
}
